// chessmate的文件操作类
import fs from 'fs'
import path from "path";

export const exportOpeningData = (exportPath: string, data: {
  name: string,
  subName: string,
  data: { fen: string, move: string, description: string }[]
}) => {
  const filePath = path.join(exportPath, './开局.json')
  if (!fs.existsSync(filePath)) {
    fs.writeFileSync(filePath, JSON.stringify({data: []}))
  }
  let fileData: any = fs.readFileSync(filePath)
  try {
    fileData = JSON.parse(fileData)
  } catch {
    fileData = {data: []}
  }
  fileData.data.push(data)
  fs.writeFileSync(filePath, JSON.stringify(fileData, null, 4))
}